package iit.projects.etaxi.view.bean;

import iit.projects.etaxi.model.entity.Customer;
import iit.projects.etaxi.model.entity.Operator;
import iit.projects.etaxi.model.service.CustomerService;
import iit.projects.etaxi.model.service.OperatorService;
import iit.projects.etaxi.model.service.ServiceLocator;
import iit.projects.etaxi.util.Constants;
import iit.projects.etaxi.util.JSFUtils;

import java.util.HashMap;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.icesoft.faces.component.ext.HtmlInputSecret;
import com.icesoft.faces.component.ext.HtmlInputText;

public class LoginBackingBean {
	
	private String username;
	private String password;
	
	private HtmlInputText compUsername;
	private HtmlInputSecret compPassword;
	
	private ServiceLocator serviceLocator;
	private CustomerService customerService;
	private OperatorService operatorService;
	
	@javax.annotation.PostConstruct
	public void init(){
		customerService = serviceLocator.getCustomerService();
		operatorService = serviceLocator.getOperatorService();
	}
	
	public String login(){
		
		if(!valid()){
			return null;
		}
		
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			Customer customer = customerService.findByUsername(this.username);
			if(customer != null){
				if(customer.getPassword().equals(password)){
					if(!customer.isSuspended()){
						map.put(Constants.LOGGED_IN_AS_CUSTOMER, Boolean.TRUE);
						map.put(Constants.CUSTOMER, customer);
						map.put(Constants.USERNAME, username);
					}
					else{
						JSFUtils.addFacesErrorMessage(compUsername, "account.suspended");
						return null;
					}
				}
				else{
					JSFUtils.addFacesErrorMessage(compUsername, "password.invalid");
					return null;
				}
			}
			else{
				Operator operator = operatorService.findByUserName(this.username);
				if(operator != null){
					if(operator.getPassword().equals(password)){
						map.put(Constants.LOGGED_IN_AS_OPERATOR, Boolean.TRUE);
						map.put(Constants.OPERATOR, operator);
						map.put(Constants.USERNAME, username);
					}
					else{
						JSFUtils.addFacesErrorMessage(compUsername, "password.invalid");
						return null;
					}
				}
				else{
					JSFUtils.addFacesErrorMessage(compUsername, "password.invalid");
					return null;
				}
			}
			map.put(Constants.LOGGED_IN, Boolean.TRUE);
			JSFUtils.storeOnSession(map);
		}catch(Throwable e){
			return null;
		}
		return "home";
	}
	
	public String logOut(){
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Constants.LOGGED_IN, Boolean.FALSE);
		map.put(Constants.LOGGED_IN_AS_OPERATOR, Boolean.FALSE);
		map.put(Constants.LOGGED_IN_AS_CUSTOMER, Boolean.FALSE);
		map.put(Constants.CUSTOMER, null);
		
		JSFUtils.storeOnSession(map);
		
		FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext ectx = ctx.getExternalContext();
        HttpSession session = (HttpSession)ectx.getSession(false);
        session.invalidate();
        ctx.responseComplete();  
        return "home";
	}
	
	private boolean valid(){
		boolean valid = true;
		if((username == null || username.isEmpty()) || 
		   (password == null || password.isEmpty())){
			JSFUtils.addFacesErrorMessage(compUsername, "values.are.required");
			valid = false;
		}
		return valid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setServiceLocator(ServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}

	public HtmlInputText getCompUsername() {
		return compUsername;
	}

	public void setCompUsername(HtmlInputText compUsername) {
		this.compUsername = compUsername;
	}

	public HtmlInputSecret getCompPassword() {
		return compPassword;
	}

	public void setCompPassword(HtmlInputSecret compPassword) {
		this.compPassword = compPassword;
	}
	
	

}
